package com.next.androidintentlibrary;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.app.Context;

public class ContactIntents {
    private Context context;
    private Intent intent;

    private ContactIntents(Context context)
    {
        this.context = context;
    }

    public static ContactIntents from(Context context)
    {
        return new ContactIntents(context);
    }

    public ContactIntents openContacts()
    {
        intent = new Intent();
        intent.setAction("android.intent.action.MAIN");
        intent.addEntity("android.intent.category.APP_CONTACTS");
        intent.setFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        return this;
    }

    public ContactIntents pickContact()
    {
        //intent = new Intent(Intent.ACTION_PICK);
        intent = new Intent();
        intent.setAction("android.intent.action.PICK");
        intent.setType("vnd.android.cursor.dir/contact");
        return this;
    }

    public ContactIntents pickSpecificContactData()
    {
        intent = new Intent();
        intent.setAction("android.intent.action.PICK");
        intent.setType("vnd.android.cursor.dir/phone_v2");
        return this;
    }

    public ContactIntents viewContact(String name)
    {
        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        //intent.setUri(Uri.)；

        return this;
    }



//    private Uri getUriOfContactByName(String name)
//    {
//        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
//        Cursor cursor = context.getContentResolver().query(uri, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
//        long idContact;
//        long idFindedContact = -1;
//        cursor.moveToFirst();
//        while (cursor.moveToNext())
//        {
//            idContact = cursor.getLong(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
//            if (cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)).equals(name))
//            {
//                idFindedContact = idContact;
//                break;
//            }
//        }
//        Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, idFindedContact);
//        return contactUri;
//    }



    public Intent build()
    {
        return intent;
    }

    private void startAbility(Intent intent)
    {
        if (!(context instanceof Ability))
        {
            intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        }
        context.startAbility(intent,0);
    }

    public void show()
    {
        startAbility(build());
    }
}
